package com.cloudkinto.service.stock.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cloudkinto.common.constant.SysConstant;
import com.cloudkinto.common.exception.BizException;
import com.cloudkinto.common.exception.BizExceptionI18;
import com.cloudkinto.common.utils.TimeUtils;
import com.cloudkinto.common.utils.dict.StaticDict;
import com.cloudkinto.common.web.PageResult;
import com.cloudkinto.common.web.SingleResult;
import com.cloudkinto.dao.*;
import com.cloudkinto.entity.*;
import com.cloudkinto.service.finance.CostRecordService;
import com.cloudkinto.service.shop.ShopService;
import com.cloudkinto.service.stock.StockLogService;
import com.cloudkinto.service.stock.StockTransferService;
import com.cloudkinto.service.stock.vo.transfer.*;
import com.cloudkinto.service.storage.StorageService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.*;

/**
 * <p>
 * service实现类
 * </p>
 *
 * @author 张永远
 * @since 2020-06-11
 */
@Service
@Transactional(rollbackFor = Exception.class)//事务回滚
public class StockTransferServiceImpl extends ServiceImpl<StockTransferDao, StockTransferDo> implements StockTransferService {
    @Autowired
    private StockTransferDao dao;
    @Autowired
    private ShopDao shopDao;
    @Autowired
    private StorageDao storageDao;
    @Autowired
    private StockTransferProductDao stockTransferProductDao;
    @Autowired
    private ProductInfoDao productInfoDao;
    @Autowired
    private UserDao userDao;
    @Autowired
    private ShopService shopService;
    @Autowired
    private RoleDao roleDao;
    @Autowired
    private StockRecordDao stockRecordDao;
    @Autowired
    private StockLogDao stockLogDao;
    @Autowired
    private StockLogService stockLogService;
    @Autowired
    private StorageService storageService;
    @Autowired
    private RoleMenuRelationDao roleMenuRelationDao;
    @Autowired
    private CostRecordService mCostService;

    @Override
    public SingleResult addInit(Long currentUserId, Long companyId) {
        Map result = new HashMap();
        List<Map> shopList = shopService.selectShopListInitByUserId(currentUserId);
        result.put("shopInList", shopList);
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("company_id", companyId);
        result.put("shopOutList", shopService.selectShopListInit(null, companyId));
        List<Map> storageList = storageService.storageList();
        result.put("storageList", storageList);

        return SingleResult.success(result);
    }

    @Override
    public StockTransferAddReq add(StockTransferAddReq req, Long userId, Long companyId) {
        boolean b = add(req, userId, companyId, true);
        return req;
    }

    @Override
    public boolean add(StockTransferAddReq req, Long userId, Long companyId, boolean needCheck) {
        if (req.getOrderForm() == null) {
            req.setOrderForm(0);
        }
        addCheck(req);
        StockTransferDo entityDo = new StockTransferDo();
        entityDo.setShopInId(req.getShopInId());
        entityDo.setShopOutId(req.getShopOutId());
        entityDo.setOrderForm(req.getOrderForm());
        entityDo.setType(0);
        entityDo.setCompanyId(companyId);
        handlerAddBefore(entityDo, userId);
        if (req.getOrderForm() != null && req.getOrderForm() == 1 && companyId == 1) {
            entityDo.setOrderCost((int) getCost(req, userId));
        } else {
            entityDo.setOrderCost(0);
        }
        dao.insert(entityDo);
        for (int i = 0; i < req.getDetailList().size(); i++) {
            if (req.getDetailList().get(i).getCount() <= 0) {
                req.getDetailList().remove(i);
                i--;
            }
        }

        for (StockTransferProductAdd product : req.getDetailList()) {
            StockTransferProductDo productDo = new StockTransferProductDo();
            productDo.setCount(product.getCount());
            productDo.setProductInfoId(product.getProductId());
            productDo.setStorageId(product.getStorageId());
            productDo.setStockTransferId(entityDo.getId());
            productDo.setDeleteFlag(0);
            stockTransferProductDao.insert(productDo);
        }

        //验证是否是管理员 是管理员进行直接操作库存的加减

        boolean b = updateStockRecord(entityDo.getId(), 1, userId, needCheck);
        return b;
    }

    @Override
    public double getCost(StockTransferAddReq req, Long companyId) {
        double totalCost = 0;
        if (req.getOrderForm() != null && req.getOrderForm() == 0) {
            return 0;
        } else {
            for (StockTransferProductAdd stockTransferProductAdd : req.getDetailList()) {
                ProductInfoDo productInfoDo = productInfoDao.selectById(stockTransferProductAdd.getProductId());
                StorageDo storageDo = storageDao.selectById(stockTransferProductAdd.getStorageId());
                float itemPrice = 50;
                if (storageDo != null) {
                    itemPrice = storageDo.getDayPrice();
                }
                if (productInfoDo != null) {
                    double cost = productInfoDo.getLength() * productInfoDo.getWidth() * productInfoDo.getHeight() / 1000000 * stockTransferProductAdd.getCount() * itemPrice * 10;
                    totalCost = totalCost + cost;
                }
            }
        }
        return totalCost;
    }

    //调货更新库存
    private boolean updateStockRecord(Long id, Integer type, Long userId, boolean needCheck) {
        //验证是否是管理员 是管理员进行直接操作库存的加减
        UserDo userDo = userDao.selectById(userId);
        RoleDo roleDo = roleDao.selectById(userDo.getRoleId());

        StockTransferDo stockTransferDo = dao.selectById(id);

        if (stockTransferDo.getType() == 1 || stockTransferDo.getType() == 2) {
            throw new BizExceptionI18(SysConstant.StockTransfer_HadCheck);
        }
        String time = TimeUtils.formatTime(new Date());
        String hours = time.split(":")[0];
        if (Integer.parseInt(hours) < 3) {
            throw new BizExceptionI18(SysConstant.System_fix_time);
        }
        int count = roleMenuRelationDao.checkAuthority(userDo.getRoleId(), StaticDict.Menu_Button.StockTransfer_Examine.getValue());
        boolean flag = false;
        if (userDo.getCompanyId() == 1L && "Director".equals(roleDo.getDevCode())) {
            flag = true;
        } else if (SysConstant.clientAdmin.equals(roleDo.getDevCode()) || (userDo.getCompanyId() != 1L && count > 0)) {
            //其他公司
            flag = true;
        }

        //判断有没有审核的权限
//        if ("Director".equals(roleDo.getDevCode()) || SysConstant.clientAdmin.equals(roleDo.getDevCode()) || stockTransferDo.getShopOutId().equals(userDo.getShopId()) || count > 0) {
        if (!flag && needCheck) {
            return false;
        }
        stockTransferDo.setType(type);
        stockTransferDo.setUpdateBy(userId);
        dao.updateById(stockTransferDo);
        LambdaQueryWrapper<StockTransferProductDo> transferProductDoWrapper = new LambdaQueryWrapper<>();
        transferProductDoWrapper.eq(StockTransferProductDo::getStockTransferId, stockTransferDo.getId());
        List<StockTransferProductDo> transferProductDoList = stockTransferProductDao.selectList(transferProductDoWrapper);
        if (!CollectionUtils.isEmpty(transferProductDoList)) {
            for (StockTransferProductDo productDo : transferProductDoList) {
                LambdaQueryWrapper<StockRecordDo> wrapperOut = new LambdaQueryWrapper<>();
                wrapperOut.eq(StockRecordDo::getStorageId, productDo.getStorageId())
                        .eq(StockRecordDo::getShopId, stockTransferDo.getShopOutId())
                        .eq(StockRecordDo::getProductInfoId, productDo.getProductInfoId());
                //转出商品
                StockRecordDo stockRecordDoOut = stockRecordDao.selectOne(wrapperOut);
                if (stockRecordDoOut == null || stockRecordDoOut.getCanSellCount() < productDo.getCount()) {
                    throw new BizException(SysConstant.StockRack_Good_NotEnough);
                }
                stockRecordDoOut.setCanSellCount(stockRecordDoOut.getCanSellCount() - productDo.getCount());
                stockRecordDoOut.setUpdateTime(new Date());
                stockRecordDoOut.setUpdateBy(userId);
                // stockRecordDao.updateById(stockRecordDoOut);
                stockRecordDao.updateStockById(stockRecordDoOut.getId(), productDo.getCount());
                LambdaQueryWrapper<StockRecordDo> wrapperIn = new LambdaQueryWrapper<>();
                wrapperIn.eq(StockRecordDo::getStorageId, productDo.getStorageId())
                        .eq(StockRecordDo::getShopId, stockTransferDo.getShopInId())
                        .eq(StockRecordDo::getProductInfoId, productDo.getProductInfoId());
                //转入商品
                StockRecordDo stockRecordDoIn = stockRecordDao.selectOne(wrapperIn);
                if (stockRecordDoIn != null) {
                    stockRecordDoIn.setCanSellCount(stockRecordDoIn.getCanSellCount() + productDo.getCount());
                    stockRecordDoIn.setUpdateTime(new Date());
                    stockRecordDoIn.setUpdateBy(userId);
                    // stockRecordDao.updateById(stockRecordDoIn);
                    stockRecordDao.updateStockById(stockRecordDoIn.getId(), -productDo.getCount());
                } else {
                    stockRecordDoIn = new StockRecordDo();
                    stockRecordDoIn.setShopId(stockTransferDo.getShopInId());
                    stockRecordDoIn.setStorageId(productDo.getStorageId());
                    stockRecordDoIn.setCanSellCount(stockRecordDoIn.getCanSellCount() + productDo.getCount());
                    stockRecordDoIn.setDeleteFlag(0);
                    stockRecordDoIn.setCanotSellCount(0);
                    stockRecordDoIn.setCompanyId(stockTransferDo.getCompanyId());
                    stockRecordDoIn.setProductInfoId(productDo.getProductInfoId());
                    stockRecordDao.insert(stockRecordDoIn);
                }
            }
        }
        //添加库存日志
        insertStockLog(stockTransferDo, transferProductDoList, userId);
        if (stockTransferDo.getCompanyId() == 1) {
            mCostService.add(2, "调货仓储费", stockTransferDo.getOrderCost(), stockTransferDo.getShopInId(), stockTransferDo.getCompanyId(), stockTransferDo.getId(), 0);
        }

        return true;
    }

    private void insertStockLog(StockTransferDo stockTransferDo, List<StockTransferProductDo> stockTransferProductDoList, Long userId) {
        stockTransferProductDoList.forEach(stockTransferProductDo -> {
            //出库日志
            StockLogDo stockLogDo = new StockLogDo();
            stockLogDo.setType(1);
            stockLogDo.setThirdOrderNumber(stockTransferDo.getId().toString());
            stockLogDo.setStorageId(stockTransferProductDo.getStorageId());
            stockLogDo.setShopId(stockTransferDo.getShopOutId());
            stockLogDo.setDeleteFlag(0);
            stockLogDo.setCompanyId(stockTransferDo.getCompanyId());
            stockLogDo.setCount(-stockTransferProductDo.getCount());
            ProductInfoDo productInfoDo = productInfoDao.selectById(stockTransferProductDo.getProductInfoId());
            if (productInfoDo != null) {
                stockLogDo.setCommoditySku(productInfoDo.getCommoditySku());
            }
            stockLogDo.setProductId(stockTransferProductDo.getProductInfoId());
            LambdaQueryWrapper<StockRecordDo> wrapperOut = new LambdaQueryWrapper<>();
            wrapperOut.eq(StockRecordDo::getStorageId, stockTransferProductDo.getStorageId())
                    .eq(StockRecordDo::getShopId, stockTransferDo.getShopOutId())
                    .eq(StockRecordDo::getProductInfoId, stockTransferProductDo.getProductInfoId());
            //转出商品
            StockRecordDo stockRecordDoOut = stockRecordDao.selectOne(wrapperOut);
            if (stockRecordDoOut != null) {
                stockLogDo.setAfterCount(stockRecordDoOut.getCanSellCount());
            }
            StockLogDo inputTimeAndCount = stockLogService.getInputTimeAndCount(stockTransferDo.getShopOutId(), stockTransferProductDo.getStorageId(),
                    stockTransferDo.getCompanyId(), productInfoDo.getCommoditySku(), stockTransferProductDo.getCount());
            stockLogDo.setCreateTime(new Date());
            stockLogDo.setCreateBy(userId);
            if (inputTimeAndCount==null){
                stockLogDo.setInputTime(new Date());
            }else {
                stockLogDo.setInputTime(inputTimeAndCount.getInputTime() == null ?inputTimeAndCount.getCreateTime() : inputTimeAndCount.getInputTime());
            }

            stockLogDo.setDeleteFlag(SysConstant.DELETE_FLAG_FALSE);
            stockLogDo.setNoteType(StaticDict.STOCK_ADJUST_TYPE.TRANSFER.getValue());
            stockLogDao.insert(stockLogDo);

            //入库日志
            StockLogDo stockLogDoIn = new StockLogDo();
            stockLogDoIn.setType(2);
            stockLogDoIn.setThirdOrderNumber(stockTransferDo.getId().toString());
            stockLogDoIn.setStorageId(stockTransferProductDo.getStorageId());
            stockLogDoIn.setShopId(stockTransferDo.getShopInId());
            stockLogDoIn.setDeleteFlag(0);
            stockLogDoIn.setCompanyId(stockTransferDo.getCompanyId());
            stockLogDoIn.setCount(stockTransferProductDo.getCount());
            ProductInfoDo productInfoDoIn = productInfoDao.selectById(stockTransferProductDo.getProductInfoId());
            if (productInfoDoIn != null) {
                stockLogDoIn.setCommoditySku(productInfoDoIn.getCommoditySku());
            }
            stockLogDoIn.setProductId(stockTransferProductDo.getProductInfoId());
            LambdaQueryWrapper<StockRecordDo> wrapperIn = new LambdaQueryWrapper<>();
            wrapperIn.eq(StockRecordDo::getStorageId, stockTransferProductDo.getStorageId())
                    .eq(StockRecordDo::getShopId, stockTransferDo.getShopInId())
                    .eq(StockRecordDo::getProductInfoId, stockTransferProductDo.getProductInfoId());
            //转入商品
            StockRecordDo stockRecordDoIn = stockRecordDao.selectOne(wrapperIn);
            if (stockRecordDoIn != null) {
                stockLogDoIn.setAfterCount(stockRecordDoIn.getCanSellCount());
            }
            if (inputTimeAndCount==null){
                stockLogDoIn.setInputTime(new Date());
            }else {
                stockLogDoIn.setInputTime(inputTimeAndCount.getInputTime() == null ?inputTimeAndCount.getCreateTime() : inputTimeAndCount.getInputTime());
            }
            stockLogDoIn.setNoteType(StaticDict.STOCK_ADJUST_TYPE.TRANSFER.getValue());
            stockLogDoIn.setCreateTime(new Date());
            stockLogDoIn.setCreateBy(userId);
            stockLogDoIn.setDeleteFlag(SysConstant.DELETE_FLAG_FALSE);
            stockLogDao.insert(stockLogDoIn);


        });
    }

    @Override
    public void verify(StockTransferVerifyReq req, Long userId) {
        boolean flag = updateStockRecord(req.getId(), req.getType(), userId, true);
        if (!flag) {
            throw new BizExceptionI18(SysConstant.NoPower_Concat);
        }
    }

    //新增校验
    private void addCheck(StockTransferAddReq req) {
        if (req.getShopInId().equals(req.getShopOutId())) {
            throw new BizExceptionI18(SysConstant.StockTransfer_ShopSame);
        }

        for (int i = 0; i < req.getDetailList().size(); i++) {
            StockTransferProductAdd stockTransferProductAdd = req.getDetailList().get(i);
            LambdaQueryWrapper<StockRecordDo> wrapperOut = new LambdaQueryWrapper<>();
            wrapperOut.eq(StockRecordDo::getStorageId, stockTransferProductAdd.getStorageId())
                    .eq(StockRecordDo::getShopId, req.getShopOutId())
                    .eq(StockRecordDo::getProductInfoId, stockTransferProductAdd.getProductId());
            //转出商品
            StockRecordDo stockRecordDoOut = stockRecordDao.selectOne(wrapperOut);
            if (stockRecordDoOut == null) throw new BizExceptionI18(SysConstant.StockTransfer_MsgError, i + 1);
            if (stockRecordDoOut.getCanSellCount() < stockTransferProductAdd.getCount()) {
                ProductInfoDo productInfoDo = productInfoDao.selectById(stockTransferProductAdd.getProductId());
                throw new BizExceptionI18(SysConstant.Stock_NotEnough, productInfoDo.getCommoditySku());
            }
        }

    }

    //做新增之前要执行的业务定制处理
    private void handlerAddBefore(StockTransferDo entityDo, Long userId) {
        entityDo.setCreateBy(userId);
        entityDo.setCreateTime(new Date());
        entityDo.setDeleteFlag(SysConstant.DELETE_FLAG_FALSE);
    }

    //将新增的do转换成要返回的实体
    private Object handlerAddAfter(StockTransferDo entityDo) {
        Object res = new Object();
        BeanUtils.copyProperties(entityDo, res);
        return res;
    }

    @Override
    public SingleResult updateInit(Long id) {
        return SingleResult.success("");
    }

    @Override
    public Object update(Object req, Long userId) {
        String s = "123";
        /*StockTransferDo entityDo = dao.selectById(req.getId());
        BeanUtils.copyProperties(req, entityDo);
        handlerUpdateBefore(entityDo, userId);
        dao.updateById(entityDo);
        return handlerUpdateAfter(entityDo);*/
        return null;
    }

    //编辑校验
    private void updateCheck(Object req) {

    }

    //做修改之前要执行的业务定制处理
    private void handlerUpdateBefore(StockTransferDo entityDo, Long userId) {
        entityDo.setUpdateBy(userId);
        entityDo.setUpdateTime(new Date());
    }

    //做修改之后要执行的业务定制处理
    private Object handlerUpdateAfter(StockTransferDo entityDo) {
        Object res = new Object();
        BeanUtils.copyProperties(entityDo, res);
        return res;
    }

    @Override
    public int delete(Long id, Long userId) {
        StockTransferDo entityDo = dao.selectById(id);
        if (entityDo.getType() == 1) {
            throw new BizExceptionI18(SysConstant.StockTransfer_Delete_Error);
        }
        handlerDeleteBefore(entityDo, userId);
        dao.updateById(entityDo);
        dao.deleteById(id);

        LambdaQueryWrapper<StockTransferProductDo> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(StockTransferProductDo::getStockTransferId, id);
        stockTransferProductDao.delete(wrapper);
        return 1;
    }

    //做删除之前要执行的业务定制处理
    private void handlerDeleteBefore(StockTransferDo entityDo, Long userId) {
        entityDo.setDeleteBy(userId);
        entityDo.setDeleteTime(new Date());
    }

    @Override
    public StockTransferDetail detail(Long id) {
        StockTransferDo entityDo = dao.selectById(id);
        StockTransferDetail res = new StockTransferDetail();
        res.setId(entityDo.getId());
        ShopDo shopInDo = shopDao.selectById(entityDo.getShopInId());
        if (shopInDo != null) {
            res.setShopInName(shopInDo.getShopName());
        }
        ShopDo shopOutDo = shopDao.selectById(entityDo.getShopOutId());
        if (shopOutDo != null) {
            res.setShopOutName(shopOutDo.getShopName());
        }
//        StorageDo storageDo = storageDao.selectById(entityDo.getStorageId());
//        if (storageDo != null) {
//            res.setStorage(storageDo.getStorageName());
//        }

        UserDo userDo = userDao.selectById(entityDo.getCreateBy());
        if (userDo != null)
            res.setCreateBy(userDo.getUserName());
        res.setCreateTime(entityDo.getCreateTime());
        if (entityDo.getType() == 2) res.setType("已拒绝");
        else if (entityDo.getType() == 1) res.setType("已确认");
        else res.setType("未确认");


        List<StockTransferProductDetail> detailList = new ArrayList<>();
        LambdaQueryWrapper<StockTransferProductDo> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(StockTransferProductDo::getStockTransferId, id);
        List<StockTransferProductDo> transferProductDoList = stockTransferProductDao.selectList(wrapper);
        if (!CollectionUtils.isEmpty(transferProductDoList)) {
            transferProductDoList.forEach(e -> {
                StockTransferProductDetail detail = new StockTransferProductDetail();
                ProductInfoDo productInfoDo = productInfoDao.selectById(e.getProductInfoId());
                StorageDo storageDo = storageDao.selectById(e.getStorageId());
                if (productInfoDo != null) {
                    detail.setCommoditySku(productInfoDo.getCommoditySku());
                    detail.setProductName(productInfoDo.getProductName());
                }
                detail.setCount(e.getCount());
                if (storageDo != null)
                    detail.setStorage(storageDo.getStorageName());
                else {
                    storageDo = storageDao.selectById(entityDo.getStorageId());
                    if (storageDo != null)
                        detail.setStorage(storageDo.getStorageName());
                }
                detail.setProductId(e.getProductInfoId());
                detailList.add(detail);
            });
        }
        res.setDetailList(detailList);

        return res;
    }

    @Override
    public SingleResult pageInit(Long currentUserId, Long companyId) {
        Map result = new HashMap();

        List<Map> shopList = shopService.selectShopListInit(currentUserId, companyId);
        result.put("shopInList", shopList);
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("company_id", companyId);
        result.put("shopOutList", shopService.selectShopListInit(null, companyId));
        List<Map> storageList = new ArrayList<>();
        List<StorageDo> storageDoList = storageDao.selectList(null);
        if (!CollectionUtils.isEmpty(storageDoList)) {
            storageDoList.forEach(e -> {
                Map map = new HashMap();
                map.put("id", e.getId());
                map.put("value", e.getStorageName());
                storageList.add(map);
            });
        }
        result.put("storageList", storageList);

        return SingleResult.success(result);
    }


    @Override
    public PageResult getListPage(Map<String, String> map, Long userId) {
        QueryWrapper<StockTransferDo> wrapper = queryBuild(map);
        IPage<StockTransferDo> page = pageInit(map);
        page = this.page(page, wrapper);
        List<StockTransferPageRes> resList = handlerListAfter(page.getRecords(), userId);
        PageResult result = new PageResult(resList, (int) page.getCurrent(), (int) page.getSize(), (int) page.getTotal(), (int) page.getPages());
        return result;
    }

    //查询分页列表之后对列表数据的处理
    private List<StockTransferPageRes> handlerListAfter(List<StockTransferDo> list, Long userId) {
        List<StockTransferPageRes> result = new ArrayList<>();
        list.forEach(e -> {
            StockTransferPageRes res = new StockTransferPageRes();
            res.setId(e.getId());

            //判断是否有权限
            //如果是管理员有操作权限  且如果转出店铺=当前用户的shopId也有权限
            UserDo userDo = userDao.selectById(userId);
            res.setAuthority(0);
            if (userDo != null && e.getType() == 0) {
                RoleDo roleDo = roleDao.selectById(userDo.getRoleId());
                if (roleDo != null && (SysConstant.serverDirector.equals(roleDo.getDevCode())
                        || SysConstant.clientAdmin.equals(roleDo.getDevCode())
                        || SysConstant.serverAdmin.equals(roleDo.getDevCode()))) {
                    res.setAuthority(1);
                }
            }
            UserDo userDo1 = userDao.selectById(e.getCreateBy());
            if (userDo1 != null)
                res.setCreateBy(userDo1.getUserName());
            ShopDo shopInDo = shopDao.selectById(e.getShopInId());
            if (shopInDo != null) {
                res.setShopInName(shopInDo.getShopName());
            }
            ShopDo shopOutDo = shopDao.selectById(e.getShopOutId());
            if (shopOutDo != null) {
                res.setShopOutName(shopOutDo.getShopName());
            }
//            StorageDo storageDo = storageDao.selectById(e.getStorageId());
//            if (storageDo != null) {
//                res.setStorage(storageDo.getStorageName());
//            }
            res.setCreateTime(e.getCreateTime());
            res.setUpdateTime(e.getUpdateTime());
            if (e.getType() == 2) {
                res.setType("已拒绝");
            } else if (e.getType() == 1) {
                res.setType("已确认");
            } else {
                res.setType("未确认");
            }

            result.add(res);
        });
        return result;
    }

    private IPage<StockTransferDo> pageInit(Map<String, String> map) {
        IPage<StockTransferDo> page = new Page();
        page.setSize(StringUtils.isNotBlank(map.get("pageSize")) ? Long.parseLong(map.get("pageSize")) : 10);
        page.setCurrent(StringUtils.isNotBlank(map.get("current")) ? Long.parseLong(map.get("current")) : 1);
        return page;
    }

    private QueryWrapper<StockTransferDo> queryBuild(Map<String, String> map) {
        QueryWrapper<StockTransferDo> wrapper = new QueryWrapper<>();
        if (StringUtils.isNotBlank(map.get("shopOutId"))) {
            wrapper.eq("shop_out_id", map.get("shopOutId"));
        }
        if (StringUtils.isNotBlank(map.get("shopInId"))) {
            wrapper.eq("shop_in_id", map.get("shopInId"));
        }
        if (StringUtils.isNotBlank(map.get("storageId"))) {
            wrapper.eq("storage_id", map.get("storageId"));
        }
        if (StringUtils.isNotBlank(map.get("type"))) {
            wrapper.eq("type", map.get("type"));
        }
        wrapper.eq("company_id", map.get("companyId"));
        wrapper.orderByDesc("id");
        return wrapper;
    }


}
